<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>猜数字</title>
</head>
<body>
<input type="text" placeholder="请输入0-100之间的整数" id="i1">
<input type="button" value="猜一猜" onclick="f()">
<div>结果:<span></span></div>
<script>
    // 得到0-100的随机整数
    let x = parseInt(101 * Math.random());
    let count = 0;
    let i = document.querySelector("#i1");
    let s = document.querySelector("span");
    
    function f() {
        count++;
        if (isNaN(i.value)) {
            s.innerText = "输入错误!";
            return;
        }
        if (x > parseInt(i.value)) {
            s.innerText = "猜小了,重新来!";
        } else if (x < parseInt(i.value)) {
            s.innerText = "猜大了,重新来!";
        } else {
            s.innerText = "恭喜你,第" + count + "次猜测终于对了!";
            // 次数清零,生成新的随机数
            count = 0;
            x = parseInt(Math.random() * 101);
        }
    }
</script>
</body>
</html>